package com.llmcu.source.java8.java8inaction.chapter12;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.Temporal;

/**
 * Period常用方法
 *
 * @author liuling
 * @date 2024-12-16 17:28
 */
public class PeriodDemo {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2023, 12, 16);
        LocalDate localDate1 = LocalDate.of(2024, 12, 16);

        // 1. 通过between构建对象
        Period period = Period.between(localDate, localDate1);
        System.out.println(period);
        // a. 只支持年月日三个维度展示，且只显示当前维度差距，而不是当前维度总差距
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        // 2. 通过of构建对象
        Period.of(2, 6, 1);
        Period period2 = Period.ofYears(2);
        // 明明两年就是24个月，显示的还是月级别差距，而不是月维度总月数差距
        System.out.println(period2.getMonths());
        Period.ofMonths(6);
        Period.ofDays(1);
        Period.ofWeeks(4);

        // 3. 通过from构建对象
        Period period1 = Period.from(period);
        System.out.println("period1:" + period1);
        // e. 当前维度总差距，但只有月份,因为每个月有几天是不一样的，所以到不了日
        System.out.println(period1.toTotalMonths());
        // f. 给LocalDate、LocalDateTime加上period得到新的LocalDate、LocalDateTime
        Temporal temporal = period1.addTo(LocalDate.of(2024, 2, 29));
        System.out.println(temporal);
        System.out.println(temporal.getClass());
        Temporal temporal1 = period1.addTo(LocalDateTime.of(2024, 2, 29, 12, 4, 3));
        System.out.println(temporal1);
        System.out.println(temporal1.getClass());

        // g. period1-period2=period3：1年减去1天=P1Y-1D，而不是11个月30天？27天？29天？
        Period period3 = period.minus(Period.ofDays(1));
        System.out.println(period3);

        // h. period*5=5period
        Period period4 = period.multipliedBy(5);
        System.out.println("period4:" + period4);

        // i. 取反
        Period period5 = period.negated();
        System.out.println("period5:" + period5);

        // j. period1+period2=period3
        Period period6 = period.plus(Period.ofDays(5));
        System.out.println("period6:" + period6);

        // k. 给LocalDate、LocalDateTime加上period得到新的LocalDate、LocalDateTime
        Temporal temporal2 = period1.subtractFrom(LocalDate.of(2024, 2, 29));
        System.out.println(temporal2);
        System.out.println(temporal2.getClass());
    }
}
